package cm.tedu._05mvcboot02.exception;

import cm.tedu._05mvcboot02.common.response.JsonResult;
import cm.tedu._05mvcboot02.common.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandleIllegalArgumentException(IllegalArgumentException ex){
        String message = ex.getMessage();
        log.error("IllegalArgumentException:" + message);

        return new JsonResult(StatusCode.OPERATION_FAILED, message);
    }

    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        String message = ex.getFieldError().getDefaultMessage();
        log.error("MethodArgumentNotValidException:" + message);
        return new JsonResult(StatusCode.VALIDATED_ERROR, message);
    }

    @ExceptionHandler
    public JsonResult doHandleConstraintViolationException(ConstraintViolationException ex){
        String message = ex.getMessage().split(":")[1].trim();
        log.error("ConstraintViolationException:" + message);
        return new JsonResult(StatusCode.VALIDATED_ERROR, message);
    }

    @ExceptionHandler
    public JsonResult doHandleMissingServletRequestParameterException(MissingServletRequestParameterException ex){
        String message = ex.getMessage();
        log.error("MissingServletRequestParameterException:" + message);
        return new JsonResult(StatusCode.VALIDATED_ERROR, message);
    }

//    @ExceptionHandler
//    public JsonResult doHandleRuntimeException(RuntimeException ex){
//        String message = ex.getMessage();
//        log.error("RuntimeException:" + message);
//
//        return new JsonResult(StatusCode.OPERATION_FAILED, message);
//    }

//    @ExceptionHandler
//    public JsonResult doHandelThrowable(Throwable ex){
//        String message = ex.getMessage();
//        log.error("doHandelThrowable:"+ message);
//
//        return new JsonResult(StatusCode.OPERATION_FAILED, message);
//    }
}
